var DateUtil = require('DateUtil');
var AUTH = require('AUTH');
module.exports = function ($http) {
    var URL_API_PREFIX = '/api';
    //get or post data
    function api_call(method, uri, data) {
        data = data || {};
        for (var i in data) {
            if (data.hasOwnProperty(i) && typeof data[i] === 'function') {
                delete data[i];
            }
            //avoid empty string problem
            if (data.hasOwnProperty(i) && data[i] === '') {
                delete data[i];
            }
        }
        if (!method) throw new Error("method is null");
        if (!uri) throw new Error("url is null");
        var req = {
            method: method,
            url: URL_API_PREFIX + uri,
            headers: {
                'Authorization': AUTH.getToken(),
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            data: $.param(data)
        };
        return $http(req).error(function(text,state){
            swal.close();
            if(state == 403){
                showToast('错误:' + text,'err');
            }else{
                showToast('连接服务器失败','err');
            }
        });
    };

    // var overlay = (function () {
    //     var html = "<div class='cover'><i class='fa fa-spinner fa-pulse'></i></div>";
    //     var $overlay = $(html);
    //     return {
    //         close: function () {
    //             $overlay.remove();
    //         },
    //         show: function () {
    //             this.close();
    //             $('body').prepend($overlay);
    //         }
    //     }
    // })();

    //DAO --DATA ACCESS OBJECT
    return {

        rbac: {
            roles: function (cb_suc) {
                return api_call('get', '/rbac/roles/');
            },
            roles_for_user: function (uid) {
                return api_call('get', '/rbac/users/' + uid + '/roles/');
            },
            add_role_for_user: function (user, role, cb_suc, cb_err) {
                // overlay.show();
                api_call('post', '/rbac/users/' + user + '/roles/', {"rid": role}, function (data) {
                    overlay.close();
                    cb_suc(data);
                }, cb_err);
            },
            remove_role_for_user: function (user, role, cb_suc) {
                // overlay.show();
                api_call('delete', '/rbac/users/' + user + '/roles/' + role, {}, function (data) {
                    overlay.close();
                    cb_suc(data);
                })

            },
            reload: function () {
                return api_call('get', '/rbac/reload');
            }

        },

        sys: {
            login_user: function (cb_suc) {
                api_call('get', '/sys/login_user/', {}, cb_suc)
            },

            menu: function (cb_suc) {
                api_call('get', '/sys/menu/', {}, cb_suc)
            },

            recount_sales_vol: function (cb_suc, cb_err) {
                // overlay.show();
                api_call('get', '/sys/recount_sales_vol', {}, cb_suc, cb_err, overlay.close);
            }

            //logout: function () {
            //    api_call('get', '/sys/logout/', {}, null, null);
            //
            //    var str = location.href.replace("http://", "http://" + new Date().getTime() + ":233321@");
            //    var xmlhttp;
            //    if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
            //    else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            //    xmlhttp.onreadystatechange = function () {
            //        if (xmlhttp.readyState == 4) location.reload();
            //    };
            //    xmlhttp.open("GET", str, true);
            //    xmlhttp.setRequestHeader("Authorization", "Basic YXNkc2E6");
            //    xmlhttp.send();
            //    return false;
            //
            //}
        },

        shop: {

            all: function () {
                return api_call('get', '/shops/');
            },
            all_normal: function (cb_suc, cb_err) {
                // overlay.show();
                api_call('get', '/shops/all/normal', {}, cb_suc, cb_err, overlay.close);
            },
            add: function (add) {
                return api_call('post', '/shops/', add);
            },
            get: function (id) {
                return api_call('get', '/shops/' + id);
            },
            upd: function (shop) {
                return api_call('put', '/shops/' + shop.id, shop);
            },
            del: function (id) {
                return api_call('delete', '/shops/' + id);
            },
            sort: function (qo, cb_suc, cb_err) {
                api_call('put', '/shops/sort', qo, cb_suc, cb_err)
            }
        },

        user: {
            get: function (id) {
                return api_call('get', '/users/' + id)
            },
            all: function (cb_suc, cb_err) {
                api_call('get', '/users/', {}, cb_suc, cb_err)
            },
            add: function (user) {
                return api_call('post', '/users/', user)
            },
            upd: function (user) {
                return api_call('put', '/users/' + user.uid,user)
            },
            del: function (uid) {
                return api_call('delete', '/users/' + uid)
            },
            brief: function () {
                return api_call('get', '/brief/users')
            },
            salesmen: function (shop_id, cb_suc) {
                api_call('get', '/users/salesmen', shop_id ? {shop_id: shop_id} : {}, cb_suc)
            },
            evaluator: function (shop_id, cb_suc) {
                api_call('get', '/users/evaluator', shop_id ? {shop_id: shop_id} : {}, cb_suc)
            }
        },

        shop_user: {
            remove_user_from_shop: function (uid) {
                return api_call('delete', '/users/' + uid + '/shop');
            },
            add_user_to_shop: function (uid, shopid) {
                return api_call('post', '/users/' + uid + '/shop', {shop_id: shopid});
            },
            users_without_shop: function () {
                return api_call('get', '/users/shop/null');
            },
            users_of_shop: function (shopid) {
                return api_call('get', '/shops/' + shopid + '/users');
            }
        },

        rank: {
            all: function () {
                return api_call('get', '/ranks/');
            },

            get: function (id) {
                return api_call('get', '/ranks/' + id);
            },

            add: function (rank) {
                return api_call('post', '/ranks/', rank)
            },

            upd: function (rank) {
                return api_call('put', '/ranks/' + rank.id, rank);
            },

            del: function (id) {
                return api_call('delete', '/ranks/' + id);
            },

            roles_for_rank: function (id) {
                return api_call('get', '/ranks/' + id + '/roles/');
            },

            add_role_to_rank: function (rankid,roleid) {
                return api_call('post', '/ranks/' + rankid + '/roles/' + roleid);
            },

            remove_role_from_rank: function (rankid, roleid) {
                return api_call('delete', '/ranks/' + rankid + '/roles/' + roleid);
            },

            ranks_for_user: function (uid) {
                return api_call('get', '/users/' + uid + '/ranks/')
            },

            add_rank_to_user: function (uid, rid) {
                return api_call('post', '/users/' + uid + '/ranks/' + rid)
            },

            remove_rank_from_user: function (uid, rid) {
                return api_call('delete', '/users/' + uid + '/ranks/' + rid)
            }

        },
        survey: {
            qa: {
                add_question: function (qo, cb_suc, cb_err) {
                    overlay.show();
                    api_call('post', '/survey/qa/create_question', qo, cb_suc, cb_err, overlay.close)
                },
                add_answer:function (question_id,qo, cb_suc, cb_err) {
                    overlay.show();
                    api_call('post', '/survey/qa/create_answer/'+question_id, qo, cb_suc, cb_err, overlay.close)
                },
                search_question:function (qo, cb_suc, cb_err) {
                    overlay.show();
                    api_call('get', '/survey/qa/search_question', qo, cb_suc, cb_err, overlay.close)
                }

            },
            temp:{
                search:function (qo, cb_suc, cb_err) {
                    overlay.show();
                    api_call('get', '/survey/temp/search', qo, cb_suc, cb_err, overlay.close)
                },
                get:function (id, cb_suc, cb_err) {
                    overlay.show();
                    api_call('get', '/survey/temp/temp/'+id, {}, cb_suc, cb_err, overlay.close)
                },
                add_temp: function (qo, cb_suc, cb_err) {
                    overlay.show();
                    api_call('post', '/survey/temp/create_temp', qo, cb_suc, cb_err, overlay.close)
                },
                add_detail:function (temp_id,qo, cb_suc, cb_err) {
                    overlay.show();
                    api_call('post', '/survey/temp/create_temp_detail/'+temp_id, qo, cb_suc, cb_err, overlay.close)
                }
            },
            order:{
                search:function (qo, cb_suc, cb_err) {
                    overlay.show();
                    api_call('get', '/survey/order/search', qo, cb_suc, cb_err, overlay.close)
                },
                get:function (id, cb_suc, cb_err) {
                    overlay.show();
                    api_call('get', '/survey/order/'+id, {}, cb_suc, cb_err, overlay.close)
                },
                upd_order:function (order_id,qo, cb_suc, cb_err) {
                    overlay.show();
                    api_call('post', '/survey/order/'+order_id, qo, cb_suc, cb_err, overlay.close)
                },
                upd_order_detail:function (order_id,qo, cb_suc, cb_err) {
                    overlay.show();
                    api_call('post', '/survey/order/detail/'+order_id, qo, cb_suc, cb_err, overlay.close)
                },
                build_by_tmpl:function (tmpl_id, cb_suc, cb_err) {
                    overlay.show();
                    api_call('post', '/survey/order/build_order_by_tmpl/'+tmpl_id, {}, cb_suc, cb_err, overlay.close)
                }
            }
        }

    }

};
